package com.mes.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.dorado.data.provider.Page;
import com.mes.common.util.SysUtil;
import com.mes.schedule.domain.ScheduleScheme;
/*import com.mes.common.util.MMUtils;
import com.mes.domain.schedule.ScheduleScheme;
import com.mes.manager.schedule.ScheduleSchemeManager;
import com.mes.manager.schedule.impl.ScheduleManager;*/
import com.mes.schedule.manager.ScheduleSchemeManager;

@Component("scheduleUtil")
@Transactional
public class ScheduleUtil {

	@Resource
	private ScheduleSchemeManager scheduleSchemeManager;

	@Resource
	private SysUtil sysUtil;

	/**
	 * 用于分页将数据返回到页面
	 * 
	 * @param page
	 * @param list
	 */
	public static <T> void initPage(Page<T> page, List<T> list) {
		if (list == null) {
			System.out.println("没有可以显示数据");
		} else {
			List<T> tempList = new ArrayList<>();
			//page.setEntities(list);
			int pageNo = page.getPageNo();
			int pageSize = page.getPageSize();
			page.setEntityCount(list.size());
			for (int i = (pageNo - 1) * pageSize; (i < pageNo * pageSize) && (i < list.size()); i++) {
				tempList.add(list.get(i));
			}
			page.setEntities(tempList);
		}

	}

	// 可以调用缺省的调度方案
	public ScheduleScheme getScheduleScheme(Map<String, Object> parameters) throws Exception {
		String scheduleUid = (String) parameters.get("scheduleUid");
		if (scheduleUid != null) {
			try {
				ScheduleScheme scheme = scheduleSchemeManager.getScheduleScheme(scheduleUid);
				return scheme;
			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("未找到调度方案，请尝试重新登录系统!!");
			}
		}

		return null;
	}

	// 可以用缺省的调度方案编号
	public String getScheduleUid(Map parameters) throws Exception {
		String scheduleUid = (String) parameters.get("scheduleUid");
		if (scheduleUid == null || scheduleUid.equals("")) {
			if (sysUtil.getSessionDeptUid() == null)
				throw new Exception("服务器连接已失效，请您重新登录！");
			scheduleUid = getScheduleUid(sysUtil.getSessionDeptUid());
			// parameters.put("scheduleUid", scheduleUid);
		}

		return scheduleUid;
	}

	// 可以用缺省的调度方案编号
	public String getScheduleUid(String deptUid) throws Exception {
		// return "1bd8d6e5-048d-4bce-bd6c-0e68096b6d43";

		List<ScheduleScheme> scheduleSchemes = scheduleSchemeManager.getDeptActiveSchemes(deptUid);
		if (scheduleSchemes != null && !scheduleSchemes.isEmpty()) {
			return scheduleSchemes.get(0).getScheduleUid();
		}

		return "schedule-" + deptUid;
	}

	/**
	 * 分页查询,由于传入进来的page肯定是非空的，并且必须在前段设置了pagesize
	 * 并且dorado可自动计算pageCount,因此也不用计算page的数目
	 * 
	 * @param <T>
	 */
	public <T> void pagePro(Page<T> page, Criteria crit) {

		crit.setProjection(Projections.rowCount());

		// set entity's number
		Long number = (Long) crit.uniqueResult();
		String count = number.toString();
		int entityCount = Integer.parseInt(count);
		page.setEntityCount(entityCount);

		// 设置开始查询的序号和查询实体的数目
		int pageNo = page.getPageNo();
		int pageSize = page.getPageSize();
		int start = (pageNo - 1) * pageSize + 1;

		crit.setFirstResult(start);
		crit.setMaxResults(pageSize);
	}

}
